package com.buer.storage.service.impl;

import com.buer.storage.dao.DorderDao;
import com.buer.storage.dao.DorderdetailsDao;
import com.buer.storage.dao.StockDao;
import com.buer.storage.dto.*;
import com.buer.storage.entity.Dorder;
import com.buer.storage.entity.DorderDO;
import com.buer.storage.entity.DorderdetailsDO;
import com.buer.storage.entity.Stock;
import com.buer.storage.service.DorderService;
import com.buer.storage.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
public class DorderServiceImpl implements DorderService {
    private SnowFlake snowFlake = new SnowFlake(2, 3);
    @Resource
    private DorderDao dorderDao;

    @Autowired
    private DorderdetailsDao dorderdetailsDao;

    @Autowired
    private StockDao stockDao;

    @Override
    public List<Stock> findStock() {
        return dorderDao.findStock();
    }

    @Override
    public PageInfo<Dorder> selectByAll(Map<String, Object> map) {

        PageHelper.startPage(map); //开始起始页
        List<Dorder> list = dorderDao.selectByAll(map); // 获取数据
        PageInfo<Dorder> page = new PageInfo<>(list); // 实例化PageInfo
        return page;

    }

    @Transactional
    @Override
    public int insert(OutStorageDto outStorageDto) {
        String orderNo = String.valueOf(snowFlake.nextId());
        outStorageDto.setDorderNum("OS" + orderNo);
        //outStorageDto.setOutStatus(0);
        int result = dorderDao.insertOrder(outStorageDto);
        for (int i = 0; i < outStorageDto.getOutProds().length; i++) {
            DorderdetailsDto outProd = outStorageDto.getOutProds()[i];
            outProd.setDorderOutid(outStorageDto.getOutId());

            Integer prodNum = outProd.getProdNum();
            Stock stock = dorderDao.queryBuerStockByParam(outProd);
            Integer u = prodNum + stock.getProdAwaitnum();
            int q = stock.getProdStocknum();
            Integer w = q - u;
            stock.setProdAwaitnum(u);
            stock.setProdResiduenum(w);
            dorderDao.updateAwaitNum(stock);
            System.out.println(stock);

        }
        dorderDao.insertDetails(outStorageDto.getOutProds());

        Integer outId = outStorageDto.getOutId();
        DorderdetailsDto[] order = dorderDao.findDordertailsAll(outId);

        for (int q = 0; q < order.length; q++) {

            DorderdetailsDto outProd = order[q];
            System.out.println(1);
            Stock stock = dorderDao.queryBuerStockByParam(outProd);
            Integer prodNum = outProd.getProdNum();
            int u = prodNum + stock.getProdAwaitnum();
            stock.setProdAwaitnum(u);
        }

        return result;
    }

    @Override
    public int insertBatch(BatchOrderDto batchOrderDto) {
        Integer stockId = batchOrderDto.getStockId();

        Stock stock = dorderDao.selectStockId(stockId);

            Integer prodNum = batchOrderDto.getProdNum();
//          Integer u = prodNum + stock.getProdAwaitnum();
            int q = stock.getProdStocknum();
            Integer w = q - prodNum;
            stock.setProdAwaitnum(prodNum);
            stock.setProdResiduenum(w);
            dorderDao.updateAwaitNum(stock);
        DorderdetailsDto dorderdetailsDto=new DorderdetailsDto();
        dorderdetailsDto.setProdCode(stock.getProdCode());
//        dorderdetailsDto.setDorderOutid(stock.getOutId());
        dorderdetailsDto.setProdName(stock.getProdName());
        dorderdetailsDto.setProdNum(batchOrderDto.getProdNum());
        dorderdetailsDto.setDorderOutid(batchOrderDto.getOutId());
        dorderdetailsDto.setProdImg(stock.getProdImg());
        dorderdetailsDto.setProdResiduenum(stock.getProdStocknum());
        dorderdetailsDto.setProdSpecial(stock.getProdSpecial());
        dorderdetailsDto.setProdPrice(stock.getProdPrice());
        dorderdetailsDto.setEmit(stock.getEmit());
        dorderdetailsDto.setProdArea(stock.getProdArea());
        dorderdetailsDto.setProdDepart(stock.getProdDepart());
        dorderdetailsDto.setCategoryTwoId(stock.getCategoryTwoId());
        dorderdetailsDto.setStockId(stock.getStockId());

        dorderDao.insertBatch(dorderdetailsDto);

        Integer outId = batchOrderDto.getOutId();
        DorderdetailsDto[] order = dorderDao.findDordertailsAll(outId);

        for (int y = 0; y < order.length;y++){
            DorderdetailsDto outProdd = order[y];
            Stock stock1 = dorderDao.queryBuerStockByParam(outProdd);
            Integer prodNum1 = batchOrderDto.getProdNum();
            int u = prodNum1 + stock1.getProdAwaitnum();
            stock.setProdAwaitnum(u);
        }
        return  stockId;
    }

    @Override
    public int updateBatch(BatchOrderDto batchOrderDto) {
        Integer outId = batchOrderDto.getOutId();
        DorderdetailsDto[] outProds = dorderDao.findDordertailsAll(outId);

        int result = 0;

        for (int i = 0; i < outProds.length; i++) {
            result = 1;
            DorderdetailsDto outProd = outProds[i];

            Integer prodNum = batchOrderDto.getProdNum();
            Stock stock = dorderDao.queryBuerStockByParam(outProd);
//            Integer u = prodNum + stock.getProdAwaitnum();
            int q = stock.getProdStocknum();
            Integer w = q - prodNum;
            stock.setProdAwaitnum(prodNum);
            stock.setProdResiduenum(w);
            outProd.setProdResiduenum(stock.getProdResiduenum());
            Integer prodResiduenum = outProd.getProdResiduenum();
            System.out.println(prodResiduenum);
            dorderDao.updateR(outProd);
            dorderDao.updateAwaitNum(stock);

//
//            DorderDto dorderDto = new DorderDto();
//            dorderDto.setOutTypeid(dorderDO.getOutTypeid());
//            dorderDto.setOutPerson(dorderDO.getOutPerson());
//            dorderDto.setOutDate(dorderDO.getOutDate());
//            dorderDto.setOutRemark(dorderDO.getOutRemark());

            dorderDao.updateBatch(batchOrderDto);
        }
        Integer outIds = batchOrderDto.getOutId();
        DorderdetailsDto[] order = dorderDao.findDordertailsAll(outIds);

        for (int q = 0; q < order.length;q++){

            DorderdetailsDto outProd = order[q];
            Stock stock = dorderDao.queryBuerStockByParam(outProd);
            Integer prodNum = outProd.getProdNum();
            int u = prodNum + stock.getProdAwaitnum();
            stock.setProdAwaitnum(u);
        }

        return result;
    }

    @Override
    public int updateOrder(OutBatchOrderDto outBatchOrderDto) {

        int update = dorderDao.update(outBatchOrderDto);

        return update;
    }

    @Override
    public int deleteBatch(BatchOrderDto batchOrderDto) {

        Stock stock1 = stockDao.get(batchOrderDto.getStockId());
        stock1.setProdAwaitnum(stock1.getProdAwaitnum()-batchOrderDto.getProdNum());
        stock1.setProdResiduenum(stock1.getProdStocknum()-stock1.getProdAwaitnum());
        int update = stockDao.update(stock1);
        if(update>0){
            dorderdetailsDao.remove(batchOrderDto.getDeliveryId());
        }

//        Integer outId = batchOrderDto.getOutId();
//        DorderdetailsDto[] outProds = dorderDao.findDordertailsAll(outId);
//
//        int result = 0;
//
//        for (int i = 0; i < outProds.length; i++) {
//            result = 1;
//            DorderdetailsDto outProd = outProds[i];
//            Stock stock = dorderDao.queryBuerStockByParam(outProd);
//            Integer prodNum = outProd.getProdNum();
//            Integer prodResiduenum = stock.getProdResiduenum();
//            int o = prodNum + prodResiduenum;
//            stock.setProdResiduenum(o);
//            Integer prodAwaitnum = stock.getProdAwaitnum();
//            int p = prodAwaitnum - prodNum;
//            stock.setProdAwaitnum(p);
//            dorderDao.updateAwaitNum(stock);
//        }
//
//        dorderDao.deleteBatch(batchOrderDto);
        return 1;

    }

    @Override
    public List<DorderDto> findOrderAll() {
        List<DorderDto> outAll = dorderDao.findOrderAll();
        return outAll;
    }

    @Transactional
    @Override
    public int deleteOutStorageById(int outId) {
        DorderdetailsDto[] outProds = dorderDao.findDordertailsAll(outId);

        int result = 0;

        for (int i = 0; i < outProds.length; i++) {
            result = 1;
            DorderdetailsDto outProd = outProds[i];
            Stock stock = dorderDao.queryBuerStockByParam(outProd);
            Integer prodNum = outProd.getProdNum();
            Integer prodResiduenum = stock.getProdResiduenum();
            int o = prodNum + prodResiduenum;
            stock.setProdResiduenum(o);
            Integer prodAwaitnum = stock.getProdAwaitnum();
            int p = prodAwaitnum - prodNum;
            stock.setProdAwaitnum(p);
            dorderDao.updateAwaitNum(stock);
        }
        dorderDao.deleteOutStorageById(outId);
        return result;
    }

    @Override
    public int deleteDorderdetails(int deliveryId) {

        int deletedorderdetails = dorderDao.deleteDorderdetails(deliveryId);
        return deletedorderdetails;
    }

    @Override
    @Transactional
    public int updateDorderdetails(OutStorageDto outStorageDto) {
        Integer outId = outStorageDto.getOutId();
        dorderDao.deleteDorderByOutId(outId);
        dorderDao.deleteDorderDetailsByOutId(outId);
        // 第二部再插入
        //outStorageDto.setOutStatus(1);
        int result = dorderDao.insertOrder(outStorageDto);
        for (int i = 0; i < outStorageDto.getOutProds().length; i++) {
            DorderdetailsDto outProd = outStorageDto.getOutProds()[i];
            outProd.setDorderOutid(outStorageDto.getOutId());

            Integer prodNum = outProd.getProdNum();
            Stock stock = dorderDao.queryBuerStockByParam(outProd);
//            Integer u = prodNum + stock.getProdAwaitnum();
            int q = stock.getProdStocknum();
            Integer w = q - prodNum;
            stock.setProdAwaitnum(prodNum);
            stock.setProdResiduenum(w);
            dorderDao.updateAwaitNum(stock);
            System.out.println(stock);
        }

        dorderDao.insertDetails(outStorageDto.getOutProds());

        Integer outIds = outStorageDto.getOutId();
        DorderdetailsDto[] order = dorderDao.findDordertailsAll(outIds);

        for (int q = 0; q < order.length;q++){

            DorderdetailsDto outProd = order[q];
            Stock stock = dorderDao.queryBuerStockByParam(outProd);
            Integer prodNum = outProd.getProdNum();
            int u = prodNum + stock.getProdAwaitnum();
            stock.setProdAwaitnum(u);
        }

        return result;
    }


    //根据订单表id查询入库信息
    @Override
    public List<OutStorageAllDto> findOutAll(int outId) {
        List<OutStorageAllDto> all = dorderDao.findOutAll(outId);
        if (all.size() > 0) {
            for (int i = 0; i < all.size(); i++) {
                DorderdetailsDto[] info = dorderDao.findOrderDetailInfoByOutId(all.get(i).getOutId());
                if (info.length > 0) {
                    all.get(i).setOutProds(info);
                }
            }
        }
        return all;
    }

    //出库单表减去
    @Override
    @Transactional
    public int submitAddBuerStock(int outId) {
        DorderdetailsDto[] outProds = dorderDao.findDordertailsAll(outId);

        int result = 0;
        for (int i = 0; i < outProds.length; i++) {
            result = 1;
            DorderdetailsDto outProd = outProds[i];

            //先查找prodArea,prodDepart,prodId
            Stock stock = dorderDao.queryBuerStockByParam(outProd);

            //如果stock表里没有的话就插入
            // dorderDao.insertStock(outProd);

            //更新累加
            Integer prodNum = outProd.getProdNum();

            Integer newProdNum = stock.getProdStocknum() - prodNum;
            Integer stockId = stock.getStockId();
            //System.out.println(stockId);
            stock.setProdStocknum(newProdNum);

            Integer prodAwaitnum = stock.getProdAwaitnum();
            int o = prodAwaitnum - prodNum;
            int q = stock.getProdStocknum();

            Integer w = q - o;
            stock.setProdAwaitnum(o);
            stock.setProdResiduenum(w);
            dorderDao.updateAwaitNum(stock);
            if (newProdNum == 0) {
                dorderDao.deleteStock(stockId);
            } else {
                dorderDao.updateAddSumStock(stock);
                dorderDao.updateStatus(outId);
            }
        }

        return result;
    }

    @Override
    public PageInfo<DorderDto> getDord(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<DorderDto> list = dorderDao.getDord(map); // 获取数据
        PageInfo<DorderDto> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    @Override
    public PageInfo<DorderdetailsDto> getDorderdetails(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<DorderdetailsDto> list = dorderDao.getDorderdetails(map); // 获取数据
        PageInfo<DorderdetailsDto> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    @Override
    public PageInfo<StockDto> selectByDate(Map<String, Object> map) {

        PageHelper.startPage(map); //开始起始页
        List<StockDto> list = dorderDao.getDate(map); // 获取数据
        PageInfo<StockDto> page = new PageInfo<>(list); // 实例化PageInfo
        return page;

    }

    @Override
    public Dorder getOne(Map<String, Object> map) {
        return dorderDao.getOne(map);
    }

    @Override
    public int save(DorderDO dorder) {
        return dorderDao.save(dorder);
    }
}
